package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.po.Clazz;
import com.itheima.domain.po.Student;
import com.itheima.domain.query.StudentQueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;

    @Override
    public PageResult studentPage(StudentQueryParam studentQueryParam) {
        //参数校验
        Integer page = studentQueryParam.getPage();
        Integer pageSize = studentQueryParam.getPageSize();
        if (page == null || pageSize == null) {
            throw new RuntimeException("参数错误");
        }
        Page<Student> studentPage = Page.of(page, pageSize);
        //分页查询
        String name = studentQueryParam.getName();
        Integer degree = studentQueryParam.getDegree();
        Integer clazzId = studentQueryParam.getClazzId();
//        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>()
//                .like(name != null, "name", name)
//                .eq(degree != null, "degree", degree)
//                .eq(clazzId != null, "clazz_id", clazzId)
//                .orderByDesc("update_time");
//        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<Student>()
//                .like(name != null, Student::getName, name)
//                .eq(degree != null, Student::getDegree, degree)
//                .eq(clazzId != null, Student::getClazzId, clazzId)
//                .orderByDesc(Student::getUpdateTime);
//        this.page(studentPage, studentLambdaQueryWrapper);
        this.lambdaQuery()
                .like(StringUtils.isNotBlank(name), Student::getName, name)
                .eq(degree != null, Student::getDegree, degree)
                .eq(clazzId != null, Student::getClazzId, clazzId)
                .orderByDesc(Student::getUpdateTime)
                .page(studentPage);
        List<Student> records = studentPage.getRecords();
        if (records != null) {
            records.forEach(student -> {
                Clazz clazz = clazzService.getById(student.getClazzId());
                student.setClazzName(clazz != null ? clazz.getName() : "未知班级");
            });
        }
        return new PageResult(studentPage.getTotal(), records);

    }


    @Override
    public void saveStudent(Student student) {
//        student.setCreateTime(LocalDateTime.now());
//        student.setUpdateTime(LocalDateTime.now());
        this.save(student);
    }

    @Override
    public void updateStudent(Student student) {
//        student.setUpdateTime(LocalDateTime.now());
        this.updateById(student);
    }

    @Override
    public void updateViolatuion(Integer id, Integer score) {
        LambdaUpdateWrapper<Student> studentLambdaUpdateWrapper = Wrappers.<Student>lambdaUpdate().eq(Student::getId, id)
                .setSql("violation_score = violation_score + " + score)
                .setSql("violation_count = violation_count + 1");
        this.update(studentLambdaUpdateWrapper);
    }
}
